﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Script.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Script class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the Script class.
    /// </summary>
    public static class Script
    {
        /// <summary>
        /// Initializes a new instance of the Script class.
        /// </summary>
        /// <param name="scriptId">The identity of the database table's row referencing this script.</param>
        /// <returns>An instantiated Script.</returns>
        public static BusinessObjects.Script Select(int scriptId)
        {
            BusinessObjects.Script script = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetScriptInfo", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ID", scriptId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            script = new BusinessObjects.Script();
                            rdr.Read();
                            script.Id = scriptId;
                            script.ScriptType = rdr["ScriptType"].ToString() == "True" ? ScriptType.Css : ScriptType.JS;
                            script.Name = (string)rdr["ScriptName"];
                            script.ScriptText = rdr["ScriptText"].ToString();
                        }
                    }
                }
            }

            return script;
        }

        /// <summary>
        /// Gets a dictionary of scripts for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="scriptType">The type of scripts to return (CSS or JS).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced script's name.</returns>
        public static List<BusinessObjects.Script> SelectAllWithoutText(ScriptType scriptType)
        {
            BusinessObjects.Script script = null;
            List<BusinessObjects.Script> scripts = new List<BusinessObjects.Script>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetScripts", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ScriptType", scriptType == ScriptType.Css ? 1 : 0);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                script = new BusinessObjects.Script();
                                script.Id = Convert.ToInt32(rdr["ScriptID"], CultureInfo.InvariantCulture);
                                script.Name = (string)rdr["scriptName"];
                                script.ScriptType = scriptType;
                                scripts.Add(script);
                            }
                        }
                    }
                }
            }

            return scripts;
        }

        /// <summary>
        /// Inserts the Script object into the database.
        /// </summary>
        /// <param name="script">The Script to insert.</param>
        /// <returns>The inserted Script with a newly populated Id field.</returns>
        public static BusinessObjects.Script Insert(BusinessObjects.Script script)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertScript", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ScriptType", script.ScriptType);
                    cmd.Parameters.AddWithValue("@ScriptName", script.Name);
                    cmd.Parameters.AddWithValue("@ScriptText", script.ScriptText ?? string.Empty);
                    script.Id = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return script;
        }

        /// <summary>
        /// Updates the Script's database fields.
        /// </summary>
        /// <param name="script">The Script to update.</param>
        public static void Update(BusinessObjects.Script script)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.UpdateScript", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ScriptID", script.Id);
                    cmd.Parameters.AddWithValue("@ScriptName", script.Name);
                    cmd.Parameters.AddWithValue("@ScriptType", script.ScriptType);
                    cmd.Parameters.AddWithValue("@ScriptText", script.ScriptText);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Deletes the Script's database entry and nullifies and page references to this script.
        /// </summary>
        /// <param name="script">The Script to delete.</param>
        public static void Delete(BusinessObjects.Script script)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                if (script.ScriptType == ScriptType.Css)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteCSS", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", script.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
                else if (script.ScriptType == ScriptType.JS)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteJS", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", script.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
    }
}